package com.atguigu.day06;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

public class Flink03_TimeWindow_Tumbling_EventTime_WaterMark_Bounded_Lateness_SideOutPut {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口获取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据按照“，”切分并转为JavaBean
        SingleOutputStreamOperator<WaterSensor> waterSensorDStream = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1])*1000, Integer.parseInt(split[2]));
            }
        })
                //分配WaterMark然后指定事件时间戳
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //可以设置固定乱序程度
                               .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        //指定事件时间戳
                        .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                            @Override
                            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                                return element.getTs();
                            }
                        })
                );

        //4.将相同id的数据聚和到一块
        KeyedStream<WaterSensor, String> keyedStream = waterSensorDStream.keyBy(r->r.getId());

        //5.开启一个基于时间的滚动窗口，窗口大小为5s
        WindowedStream<WaterSensor, String, TimeWindow> window = keyedStream.window(TumblingEventTimeWindows.of(Time.seconds(5)))
                //允许迟到的数据
                .allowedLateness(Time.seconds(3))
                //TODO 侧输出（将关窗之后来的数据放到侧输出流中）
                .sideOutputLateData(new OutputTag<WaterSensor>("outPut"){});

        //6.使用增量聚合函数做累加计算
        SingleOutputStreamOperator<String> result = window.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
            @Override
            public void process(String key, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                String msg = "当前key: " + key
                        + "窗口: [" + context.window().getStart() / 1000 + "," + context.window().getEnd() / 1000 + ") 一共有 "
                        + elements.spliterator().estimateSize() + "条数据 ";
                out.collect(msg);
            }
        });

        result.print("主流");

        //TODO 获取侧输出流并打印
        DataStream<WaterSensor> sideOutput = result.getSideOutput(new OutputTag<WaterSensor>("outPut") {
        });

        sideOutput.print("侧输出->迟到的数据");

        env.execute();



    }
}
